package com.qzy.demo.controller;

import com.alibaba.fastjson.JSON;
import com.qzy.demo.pojo.OrderPojo;
import com.qzy.demo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/add")
    public String addOrder(OrderPojo order, ModelMap modelMap){
        log.info("*************into method add*****************");
        order.setAddTime(new Date());
        //orderService.addOrder(order);
        //Map<String,Object> map = new HashMap<String,Object>();
        modelMap.put("name","JOE");
        log.info("******************modelMap [] "+modelMap.toString());
        return "index";
    }

    @RequestMapping("/update")
    public String updOrder(OrderPojo order){
        log.info("*************into method updOrder***************** \n order[]"+ JSON.toJSONString(order));
        orderService.updOrder(order);
        return "update success!";
    }

    @RequestMapping("/query")
    public OrderPojo queryOrder(String orderNo){
        log.info("*************into method queryOrder*****************\n orderNo=="+orderNo);
        return orderService.queryOrderByNo(orderNo);
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<OrderPojo> queryAll(){
        log.info("*************into method queryAll*****************");
        List<OrderPojo> list = null ;
        list = orderService.queryOrders();
        log.info("list[]"+JSON.toJSONString(list));
        return list;
    }

    @RequestMapping("/delete")
    public String delOrder(String  orderNo){
        log.info("*************into method delOrder***************** \n orderNo"+orderNo);
        orderService.delOrders(orderNo);
        return "delete success!";
    }



}
